﻿using BaifudataPay.Applib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BaifudataPay
{
    public partial class LogView : System.Web.UI.Page
    {
        protected IList<string> dirs;
        string rootDir;
        protected string cur_dir, cur_file, action;
        protected void Page_Load(object sender, EventArgs e)
        {
            cur_dir = RequestUtil.GetString("dir");
            cur_file = RequestUtil.GetString("file");
            action = RequestUtil.GetString("action");

            rootDir = Server.MapPath("/Runtime/Log/");
            // 自动创建日志根目录
            if (!Directory.Exists(rootDir))
            {
                try
                {
                    Directory.CreateDirectory(rootDir);
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }

            if ("delete" == action)
            {
                DeleteFile(cur_dir, cur_file);
                Response.Redirect("LogView.aspx?dir=" + cur_dir, true);
            }
            else if ("deletelogs" == action)
            {
                EmptyLogs();
                Response.Redirect("LogView.aspx", true);
            }

            dirs = GetDirs();
        }

        public IList<string> GetDirs()
        {
            IList<string> list = new List<string>();
            string[] dirs = Directory.GetDirectories(rootDir);
            string filename;
            foreach (string dir in dirs)
            {
                filename = Path.GetFileName(dir);
                list.Add(filename);
            }
            return list;

        }
        public IList<string> GetFiles(string dirName)
        {
            IList<string> list = new List<string>();
            string[] files = Directory.GetFiles(rootDir + dirName);
            string filename;
            foreach (string file in files)
            {
                filename = Path.GetFileName(file);
                list.Add(filename);
            }
            return list;
        }
        public string GetFileContent(string dirName, string fileName)
        {
            string path = rootDir + dirName + Path.DirectorySeparatorChar + fileName;
            if (File.Exists(path))
            {

                try
                {
                    FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
                    return sr.ReadToEnd();
                    //return File.ReadAllText(path, System.Text.Encoding.GetEncoding("GB2312"));
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
            return string.Empty;
        }
        public void DeleteFile(string dirName, string fileName)
        {
            string path = rootDir + dirName + Path.DirectorySeparatorChar + fileName;
            if (File.Exists(path))
            {
                try
                {
                    File.Delete(path);
                }
                catch
                {
                }
            }
        }

        /// <summary>
        /// 清空所有日志
        /// </summary>
        public void EmptyLogs()
        {
            IList<string> dirs = GetDirs();
            string path;
            foreach (string dir in dirs)
            {
                path = rootDir + dir;
                if (Directory.Exists(path))
                {
                    try
                    {
                        Directory.Delete(path, true);
                    }
                    catch
                    {
                    }
                }
            }
        }
    }
}